import axios from 'axios';

const postDataFormat = (data: any) => {
  let dataString = '';
  const keys = Object.keys(data);
  const len = keys.length;
  keys.forEach((el) => {
    if (el !== keys[len - 1]) {
      dataString += `${el}=${data[el]}&`;
    } else {
      dataString += `${el}=${data[el]}`;
    }
  });
  return dataString;
};

const ajaxGet = (
    url: string, params = {},
    resolve: (res: any, ...args: any) => void,
    reject: (e: Error) => void,
    args?: string[]) => {
  args = args ? args : [];
  axios.get(url, {params})
    .then((response) => resolve(response, ...args || ''))
    .catch((e: any) => reject(e));
};

const ajaxPost = (
    url: string,
    data: any,
    resolve: (res: any) => void,
    reject: (e: Error) => void) => {
  axios.post(url, data)
    .then((response) => resolve(response))
    .catch((e) => reject(e));
};

const ajaxDel = (
    url: string, params= {},
    resolve: (res: any) => void,
    reject: (e: Error) => void) => {
  axios.delete(url, {params})
    .then((response) => resolve(response))
    .catch((e) => reject(e));
};

const genericError = (e: Error) => {
  // tslint:disable-next-line:no-console
  console.log(e);
};

export {
    ajaxGet
  , ajaxPost
  , ajaxDel
  , genericError
  ,

};

